# -*- coding: utf-8 -*-
# @Time    : 2024/8/29 8:55
# @Author  : Alvin
# @File    : users.py

from flask import g, jsonify

from app.libs.redprint import Redprint
from app.libs.token_auth import auth
from app.models.downloads import Download
from app.libs.error_code import NotFound
from app.models.users import User
api = Redprint('users')


@api.route('/download_record', methods=['GET'])
@auth.login_required
def download_record():
    uid = g.user.uid
    print(uid)
    download_records = Download.get_all_by_user_id(uid)
    if download_records:
        return jsonify(download_records)
    else:
        return NotFound(msg="没有找到任何下载记录")


@auth.login_required()
@api.route('/upload_record', methods=['GET'])
def upload_record():
    pass




# 获取当前用户信息
@auth.login_required()
@api.route('/user_info', methods=['GET'])
def get_user_info():
    # 这里直接从g中获取用户id即可，因为改接口需要鉴权，直接可以获取用户id
    uid = g.user.id
    # 这里是否要看看有无此用户是应为考虑到还没实现注销删除对应token，可能导致用户拿着旧的token来获取
    user = User.query.filter_by(id=uid).first_or_404()
    t = {
        'error_code': 0,
        'msg': '成功获取!',
        'data':{
            'userInfo':user
        }

    }
    return jsonify(t)



